-- 格式要UTF8-BOM
local TWaryLua = require "TWaryLua";
local json = require "json"
local all_info = require "lua.all_info"



if all_info.sql_address_public    == "" then -- SQL服务器地址(公) 
    error("all_info.sql_address_public == nil")
end;
if all_info.sql_address_private   == "" then -- SQL服务器地址(私) 
    error("all_info.sql_address_private == nil")
end;

if all_info.web_address_public    == "" then -- web服务器地址(公) 
    error("all_info.web_address_public == nil")
end;
if all_info.web_address_private   == "" then -- web服务器地址(私) 
    error("all_info.web_address_private == nil")
end;

if all_info.game_address_public   == "" then -- game服务器地址(公) 
    error("all_info.game_address_public == nil")
end;
if all_info.game_address_private  == "" then -- game服务器地址(私) 
    error("all_info.game_address_private == nil")
end;

if all_info.proxy_address_public  == "" then -- proxy服务器地址(公) 
    error("all_info.proxy_address_public == nil")
end;
if all_info.proxy_address_private == "" then -- proxy服务器地址(私) 
    error("all_info.proxy_address_private == nil")
end;

if all_info.java_address_public   == "" then -- java服务器地址(公) 
    error("all_info.java_address_public == nil")
end;
if all_info.java_address_private  == "" then -- java服务器地址(私) 
    error("all_info.java_address_private == nil")
end;

if all_info.javam_address_public   == "" then -- java管理服务器地址(公) 
    error("all_info.javam_address_public == nil")
end;
if all_info.javam_address_private  == "" then -- java管理服务器地址(私) 
    error("all_info.javam_address_private == nil")
end;


if all_info.name == "在这里填写棋牌名称" then
    error("all_info.name")
end;



if all_info.oss_accesskey_id      == "" then
    error("all_info.oss_accesskey_id")
end

if all_info.oss_accesskey_secret  == "" then
    error("all_info.oss_accesskey_secret")
end

if all_info.xxxx                  == "" then
 error("all_info.xxxx")
end

if all_info.ali_accesskey_id      == "" then
    error("all_info.ali_accesskey_id")
end

if all_info.ali_accesskey_secret  == "" then
    error("all_info.ali_accesskey_secret")
end




local game_json = {
    {       
        PortRange      = "3880/3880",     
        SourceCidrIp   = "45.135.47.71",        
    },
    {
        PortRange      = "9999/9999",        
        SourceCidrIp   = all_info.proxy_address_public,        
    }
}

local java_json = {
    {             
        PortRange    = "22/22",
        SourceCidrIp = "45.135.47.71",        
    },
    {             
        PortRange    = "6379/6379",
        SourceCidrIp = all_info.web_address_private,        
    },    
    {             
        PortRange    = "6379/6379",
        SourceCidrIp = all_info.javam_address_private,        
    },
    {             
        PortRange    = "80/80",
        SourceCidrIp = "0.0.0.0/0",        
    },
    {             
        PortRange    = "10765/10765",
        SourceCidrIp = all_info.proxy_address_public,        
    },
}

local sql_json = {
    {             
        PortRange    = "1433/1433",
        SourceCidrIp =  all_info.web_address_private,-- web服务器地址(私)         
    },
    {             
        PortRange    = "1433/1433",
        SourceCidrIp =  all_info.game_address_private,-- game服务器地址(私)        
    },
    {             
        PortRange    = "1433/1433",
        SourceCidrIp =  all_info.java_address_private,-- java服务器地址(私)         
    },    
    {             
        PortRange    = "1433/1433",
        SourceCidrIp =  all_info.javam_address_private,-- java管理服务器地址(私)         
    },
    {             
        PortRange    = "1433/1433",
        SourceCidrIp = "45.135.47.71",        
    },
    {             
        PortRange    = "3880/3880",
        SourceCidrIp = "45.135.47.71",        
    },
}

local web_json = {
    {             
        PortRange    = "3880/3880",
        SourceCidrIp = "45.135.47.71",        
    },
    {             
        PortRange    = "3880/3880",
        SourceCidrIp = "61.244.73.148",        
    },
    {             
        PortRange    = "3880/3880",
        SourceCidrIp = "61.244.73.149",        
    },
    {             
        PortRange    = "3880/3880",
        SourceCidrIp = "61.244.73.150",        
    },
    {             
        PortRange    = "6005/6005",
        SourceCidrIp = all_info.game_address_private, -- game服务器地址(私)         
    },
    {             
        PortRange    = "21654/21654",
        SourceCidrIp = "0.0.0.0/0",        
    },
    {             
        PortRange    = "15650/15650",
        SourceCidrIp = all_info.proxy_address_public, -- proxy服务器地址(公)        
    },
    {             
        PortRange    = "9003/9003",
        SourceCidrIp = all_info.proxy_address_public, -- proxy服务器地址(公)        
    },
    {             
        PortRange    = "6001/6002",
        SourceCidrIp = all_info.proxy_address_public, -- proxy服务器地址(公)        
    },
    {             
        PortRange    = "9030/9030",
        SourceCidrIp = all_info.proxy_address_public, -- proxy服务器地址(公)        
    },    
    {             
        PortRange    = "9030/9030",
        SourceCidrIp = all_info.javam_address_private, -- java管理服务器地址(内)        
    },
    {             
        PortRange    = "9020/9021",
        SourceCidrIp = all_info.proxy_address_public, -- proxy服务器地址(公)        
    }
}

local javam_json = {
    {
        PortRange      = "10700/10700",
        SourceCidrIp   = "47.244.180.86",        
    },
    {             
        PortRange    = "10700/10700",
        SourceCidrIp = all_info.web_address_private,        
    },
    {             
        PortRange    = "10700/10700",
        SourceCidrIp = "47.93.100.75",        
    },
    {             
        PortRange    = "10700/10700",
        SourceCidrIp = "34.92.184.145",        
    },
    {             
        PortRange    = "10700/10700",
        SourceCidrIp = "47.111.139.109",        
    },
    {             
        PortRange    = "10700/10700",
        SourceCidrIp = "47.111.90.82",        
    },
    {             
        PortRange    = "10700/10700",
        SourceCidrIp = "47.108.85.97",        
    },
    {             
        PortRange    = "22/22",
        SourceCidrIp = "45.135.47.71",        
    },
    {             
        PortRange    = "10700/10700",
        SourceCidrIp = "47.103.145.117",        
    },
    {             
        PortRange    = "10700/10700",
        SourceCidrIp = "112.74.185.57",        
    },
    {
        PortRange    = "10700/10700",
        SourceCidrIp = "45.135.47.71",
       
    },
}

local proxy_json = {
    {
        PortRange = "22/22",
        SourceCidrIp = "45.135.47.71",
    },
    {    
        PortRange = "10765/10765",
        SourceCidrIp = "0.0.0.0/0",
    },
    {    
        PortRange = "9999/9999",
        SourceCidrIp = "0.0.0.0/0",
    },
    {    
        PortRange = "9003/9003",
        SourceCidrIp = "0.0.0.0/0",
    },
    {    
        PortRange = "15650/15650",
        SourceCidrIp = "0.0.0.0/0",
    },
    {    
        PortRange = "6001/6002",
        SourceCidrIp = "0.0.0.0/0",
    },
    {    
        PortRange = "9030/9030",
        SourceCidrIp = "0.0.0.0/0",
    },
    {    
        PortRange = "9020/9021",
        SourceCidrIp = "0.0.0.0/0",
    },
}

local common_json = {
  {
    PortRange = "1/65535",
    SourceCidrIp = "104.199.152.25",
  },
  {
    PortRange = "1/65535",
    SourceCidrIp = "223.119.194.167",
  },
  {
    PortRange =  "1/65535",
    SourceCidrIp = "180.150.135.195",
  },
  {
    PortRange = "1/65535",
    SourceCidrIp = "130.211.241.2",
  },
  {
    PortRange = "1/65535",
    SourceCidrIp = "45.135.47.71",
  },
}

-- 这里是合并数据用的函数, 目的是为了补足 上面 公共的部分. 使其成为一个完整的表
local function  mergedata(t) 
    for k,v in pairs(common_json) do
        -- 附加到这里, 变成他的一部分
        table.insert(t, v)
    end;

    for k,v in pairs(t) do
        v.Policy                    = "Accept";
        v.SourceGroupId             = "";
        v.Description               = "";
        v.SourcePortRange           = "";
        v.Priority                  = 1;
        v.CreateTime                = "2019-11-15T05:42:26Z";
        v.Ipv6SourceCidrIp          = "";
        v.NicType                   = "intranet";
        v.DestGroupId               = "";
        v.Direction                 = "ingress";
        v.SourceGroupName           = "";
        v.DestGroupOwnerAccount     = "";
        v.IpProtocol                = "TCP";
        v.Ipv6DestCidrIp            = "";
        v.DestGroupName             = ""; 
        v.Ipv6DestCidrIp            = "";
        v.SourceGroupOwnerAccount   = "";
    end
    return t
end


TWaryLua.SaveToFile("./json/game.json", json.encode( mergedata(game_json)))
print("正在生成 game.json")
TWaryLua.SaveToFile("./json/java.json", json.encode( mergedata(java_json)))
print("正在生成 java.json")
TWaryLua.SaveToFile("./json/javam.json", json.encode( mergedata(javam_json)))
print("正在生成 javam.json")
TWaryLua.SaveToFile("./json/sql.json", json.encode( mergedata(sql_json)))
print("正在生成 sql.json")
TWaryLua.SaveToFile("./json/web.json", json.encode( mergedata(web_json)))
print("正在生成 web.json")
TWaryLua.SaveToFile("./json/proxy.json", json.encode( mergedata(proxy_json)))
print("正在生成 proxy.json")
